<?php
class th extends Thread{
    public $name;
    public $rw=0;
    public $isruning;
    function __construct($name){
        $this->name=$name;
    }

    function run(){
        while(true) {
            if ($this->rw > 0) {
                echo "\r\n" . $this->name . "在执行{$this->rw}任务!\n";
                sleep(5);
                echo $this->name . "执行完{$this->rw}任务!\n";
                $this->rw = 0;
            }
        }
    }
}

$p[]=new th("a");
$p[]=new th("b");
$p[]=new th("c");
$p[]=new th("d");

foreach ($p as $k => $ap) {
    $ap->start();
    if($ap->isRunning()){
        echo "\r\n".$ap->name."已经开始\r\n";
    }
}

$i=1;
do {
    while(true) {
        foreach ($p as $k=>$ap) {
            if(!$ap->rw==0){
                $ap->rw = $i;
                break(2);
            }
        }
    }
    $i++;
}while($i<=10);

echo "\ndone.";
